﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class DanhMucLoaiHang : Form
    {
        List<LoaiHang> danhSachLoaiHang;

        public DanhMucLoaiHang()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDanhsach();
            if (@Session.quyen == "2") 
            {
                btnLuu.Enabled = false;
                btnThem.Enabled = false;
            }
        }

        private void gridViewLoaiHang_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (danhSachLoaiHang != null)
            {
                LoaiHang loaiHang = danhSachLoaiHang[e.RowIndex];

                txtMaLoai.Text = loaiHang.MaLoaiHienThi.ToString();
                txtTenLoai.Text = loaiHang.TenLoai;
            }
        }

        

        private void LoadDanhsach()
        {
            danhSachLoaiHang = Business.LoaiHang.LayDanhSach();
            gridViewLoaiHang.DataSource = danhSachLoaiHang;

            gridViewLoaiHang.Columns["MaLoai"].Visible = false;
            gridViewLoaiHang.Columns["MaLoaiHienThi"].HeaderText = "Mã loại";
            gridViewLoaiHang.Columns["TenLoai"].HeaderText = "Tên loại";

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            txtTenLoai.Text = "";
            txtMaLoai.Text = "";
            txtTenLoai.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtTenLoai.Text == "") 
            {
                MessageBox.Show("Nhập đầy đủ thông tin vào loại hàng!");
                txtTenLoai.Focus();
                return;

            }
            if (txtMaLoai.Text == "")
            {
                LoaiHang.ThemMoi(new LoaiHang(txtTenLoai.Text));
                LoadDanhsach();

            }
            else 
            {
                LoaiHang.CapNhat(new LoaiHang(Int16.Parse(txtMaLoai.Text), txtTenLoai.Text));
                LoadDanhsach();
            }
        }

        private void groupBox_Enter(object sender, EventArgs e)
        {

        }
    }
}
